<!DOCTYPE html>
<html lang="zh_Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="cpmFX379Hw.chiangyung.github.io">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chiangyung.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","Pisces | Gemini":240,"width":240,"display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本文总结了从[博客](https:&#x2F;&#x2F;www.cnblogs.com&#x2F;xbf9xbf&#x2F;p&#x2F;5140675.html)中学习MPI的一些内容。">
<meta property="og:type" content="article">
<meta property="og:title" content="MPI学习记录（二）">
<meta property="og:url" content="https://chiangyung.github.io/posts/55404/index.html">
<meta property="og:site_name" content="姜勇的博客">
<meta property="og:description" content="本文总结了从[博客](https:&#x2F;&#x2F;www.cnblogs.com&#x2F;xbf9xbf&#x2F;p&#x2F;5140675.html)中学习MPI的一些内容。">
<meta property="article:published_time" content="2020-01-07T04:00:00.000Z">
<meta property="article:modified_time" content="2020-04-25T14:27:55.189Z">
<meta property="article:author" content="ChiangYung">
<meta property="article:tag" content="Programming">
<meta property="article:tag" content="MPI">
<meta property="article:tag" content="Parallel Computing">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://chiangyung.github.io/posts/55404/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh_Hans'
  };
</script>

  <title>MPI学习记录（二） | 姜勇的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="姜勇的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">姜勇的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">26</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">3</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">41</span></a>

  </li>
        <li class="menu-item menu-item-合集">

    <a href="/collections/" rel="section"><i class="fa fa-folder fa-fw"></i>合集</a>

  </li>
        <li class="menu-item menu-item-课程">

    <a href="/course/" rel="section"><i class="fa fa-graduation-cap fa-fw"></i>课程</a>

  </li>
        <li class="menu-item menu-item-读书">

    <a href="/books/" rel="section"><i class="fa fa-book fa-fw"></i>读书</a>

  </li>
        <li class="menu-item menu-item-旅行">

    <a href="/travels/" rel="section"><i class="fa fa-train fa-fw"></i>旅行</a>

  </li>
        <li class="menu-item menu-item-观影">

    <a href="/videos/" rel="section"><i class="fa fa-tv fa-fw"></i>观影</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh_Hans">
    <link itemprop="mainEntityOfPage" href="https://chiangyung.github.io/posts/55404/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/upload/E8.png">
      <meta itemprop="name" content="ChiangYung">
      <meta itemprop="description" content="我于蒙昧之中，探寻宇宙万物。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="姜勇的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MPI学习记录（二）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-01-07 12:00:00" itemprop="dateCreated datePublished" datetime="2020-01-07T12:00:00+08:00">2020-01-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-04-25 22:27:55" itemprop="dateModified" datetime="2020-04-25T22:27:55+08:00">2020-04-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Programming/" itemprop="url" rel="index"><span itemprop="name">Programming</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>12k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>11 分钟</span>
            </span>
            <div class="post-description">本文总结了从[博客](https://www.cnblogs.com/xbf9xbf/p/5140675.html)中学习MPI的一些内容。</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>从相关博客中学习MPI：<code>https://www.cnblogs.com/xbf9xbf/p/5154428.html</code>。本篇章主要从几段程序来初步学习MPI相关内容：(1)对等模式的MPI程序设计；(2)主从模式的MPI程序设计。</p>
<p>关于对等模式与主从模式，查到的一些信息主要是说对于数据操作后的同步情况，对等模式是实时同步的，而主从模式是主操作后广播同步。</p>
<blockquote>
<p>主从模式是对主操作数据，从会实时同步数据。反之对从操作，主不会同步数据，还有可能造成数据紊乱，导致主从失效。对等模式则是无论对那一台操作，另一个都会同步数据。一般用作高容灾方案。</p>
</blockquote>
<p>下面直接看代码：</p>
<h2 id="对等模式的MPI程序设计"><a href="#对等模式的MPI程序设计" class="headerlink" title="对等模式的MPI程序设计"></a>对等模式的MPI程序设计</h2><h3 id="初始设计"><a href="#初始设计" class="headerlink" title="初始设计"></a>初始设计</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 这里讲的雅可比迭代是从左到右、从上到下，每个元素等于其周围四个元素的平均值，</span></span><br><span class="line"><span class="comment"> * 依次计算。</span></span><br><span class="line"><span class="comment"> * while (not converged) &#123;</span></span><br><span class="line"><span class="comment"> *    for (i,j)</span></span><br><span class="line"><span class="comment"> *        xnew[i][j] = (x[i + 1][j] + x[i - 1][j] + x[i][j + 1] + x[i][j - 1])/4;</span></span><br><span class="line"><span class="comment"> *    for (i,j)</span></span><br><span class="line"><span class="comment"> *        x[i][j] = xnew[i][j];</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 8</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SIZE (N / 4)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T 2</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_myRows</span><span class="params">(<span class="keyword">int</span>, <span class="keyword">float</span> [][N])</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">// 这里假定用4个进程计算，每个进程存放数据的大小为[SIZE + 2][N]</span></span><br><span class="line">    <span class="keyword">float</span> myRows[SIZE + <span class="number">2</span>][N], myRows2[SIZE + <span class="number">2</span>][N];</span><br><span class="line">    <span class="keyword">int</span> myid;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    MPI_Init(&amp;argc, &amp;argv);</span><br><span class="line">    MPI_Comm_rank(MPI_COMM_WORLD, &amp;myid);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> i, j;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化，最终目的是拆分前的8*8数组的边都为8，其余为0</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; SIZE + <span class="number">2</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">            myRows[i][j] = myRows2[i][j] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">0</span> == myid) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            myRows[<span class="number">1</span>][j] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">3</span> == myid) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            myRows[SIZE][j] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; SIZE + <span class="number">1</span>; i++) &#123;</span><br><span class="line">        myRows[i][<span class="number">0</span>] = <span class="number">8.0</span>;</span><br><span class="line">        myRows[i][N - <span class="number">1</span>] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Jacobi Iteration部分</span></span><br><span class="line">    <span class="keyword">int</span> <span class="built_in">step</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="built_in">step</span> = <span class="number">0</span>; <span class="built_in">step</span> &lt; T; <span class="built_in">step</span>++) &#123;</span><br><span class="line">        <span class="comment">// 边界的数据相互传递</span></span><br><span class="line">        <span class="keyword">if</span> (myid &lt; <span class="number">3</span>) &#123;</span><br><span class="line">            <span class="comment">// 从 下方 进程接收数据</span></span><br><span class="line">            MPI_Recv(&amp;myRows[SIZE + <span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, myid + <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	MPI_COMM_WORLD, &amp;status);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (myid &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">// 向 上方 进程发送数据</span></span><br><span class="line">            MPI_Send(&amp;myRows[<span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, myid - <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (myid &lt; <span class="number">3</span>) &#123;</span><br><span class="line">            <span class="comment">// 向 下方 进程发送数据</span></span><br><span class="line">            MPI_Send(&amp;myRows[SIZE][<span class="number">0</span>], N, MPI_FLOAT, myid + <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	MPI_COMM_WORLD);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (myid &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">// 从 上方 进程接收数据</span></span><br><span class="line">            MPI_Recv(&amp;myRows[<span class="number">0</span>][<span class="number">0</span>], N, MPI_FLOAT, myid - <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	MPI_COMM_WORLD, &amp;status);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 边界层的数据传递完之后，就可以计算了，用一个新的数组接收计算后的值</span></span><br><span class="line">        <span class="keyword">int</span> r_begin, r_end;</span><br><span class="line">        r_begin = (<span class="number">0</span> == myid) ? <span class="number">2</span> : <span class="number">1</span>;</span><br><span class="line">        r_end = (<span class="number">3</span> == myid) ? SIZE - <span class="number">1</span> : SIZE;</span><br><span class="line">        <span class="keyword">for</span> (i = r_begin; i &lt;= r_end; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt; N - <span class="number">1</span>; j++)</span><br><span class="line">                myRows2[i][j] = <span class="number">0.25</span> * (myRows[i][j - <span class="number">1</span>] + myRows[i][j + <span class="number">1</span>]</span><br><span class="line">                					  + myRows[i - <span class="number">1</span>][j] + myRows[i + <span class="number">1</span>][j]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 更新</span></span><br><span class="line">        <span class="keyword">for</span> (i = r_begin; i &lt;= r_end; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt; N - <span class="number">1</span>; j++)</span><br><span class="line">                myRows[i][j] = myRows2[i][j];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// MPI_Barrier(MPI_COMM_WORLD);</span></span><br><span class="line">    print_myRows(myid, myRows);</span><br><span class="line">    MPI_Finalize();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_myRows</span><span class="params">(<span class="keyword">int</span> myid, <span class="keyword">float</span> myRows[][N])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line">    <span class="keyword">int</span> buf[<span class="number">1</span>];</span><br><span class="line">    MPI_Status status;</span><br><span class="line">    buf[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (myid &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        MPI_Recv(buf, <span class="number">1</span>, MPI_INT, myid - <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD, &amp;status);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"Result in process %d:\n"</span>, myid);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; SIZE + <span class="number">2</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"%1.3f\t"</span>, myRows[i][j]);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (myid&lt;<span class="number">3</span> ) &#123;</span><br><span class="line">        MPI_Send(buf, <span class="number">1</span>, MPI_INT, myid + <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD);</span><br><span class="line">    &#125;</span><br><span class="line">    MPI_Barrier(MPI_COMM_WORLD);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="捆绑发送"><a href="#捆绑发送" class="headerlink" title="捆绑发送"></a>捆绑发送</h3><p>主要使用<code>MPI_Sendrecv()</code>取代<code>MPI_Send()</code>和<code>MPI_Recv()</code>，实际上使用过程中，<code>MPI_Sendrecv()</code>也是效率更高更安全的一种方式。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 8</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SIZE (N / 4)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T 2</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_myRows</span><span class="params">(<span class="keyword">int</span>, <span class="keyword">float</span> [][N])</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">float</span> myRows[SIZE + <span class="number">2</span>][N], myRows2[SIZE + <span class="number">2</span>][N];</span><br><span class="line">    <span class="keyword">int</span> myid;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    MPI_Init(&amp;argc, &amp;argv);</span><br><span class="line">    MPI_Comm_rank(MPI_COMM_WORLD, &amp;myid);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line">    <span class="comment">// 初始化</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; SIZE + <span class="number">2</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">        &#123;</span><br><span class="line">            myRows[i][j] = myRows2[i][j] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">0</span> == myid) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            myRows[<span class="number">1</span>][j] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">3</span> == myid) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            myRows[SIZE][j] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; SIZE + <span class="number">1</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        myRows[i][<span class="number">0</span>] = <span class="number">8.0</span>;</span><br><span class="line">        myRows[i][N - <span class="number">1</span>] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/*Jacobi Iteration部分*/</span></span><br><span class="line">    <span class="keyword">int</span> <span class="built_in">step</span>;</span><br><span class="line">    <span class="keyword">for</span> ( <span class="built_in">step</span> = <span class="number">0</span>; <span class="built_in">step</span> &lt; T; <span class="built_in">step</span>++ )</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 从上往下平移数据</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0</span> == myid) &#123;</span><br><span class="line">            MPI_Send(&amp;myRows[SIZE][<span class="number">0</span>], N, MPI_FLOAT, myid + <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	MPI_COMM_WORLD);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">3</span> == myid) &#123;</span><br><span class="line">            MPI_Recv(&amp;myRows[<span class="number">0</span>][<span class="number">0</span>], N, MPI_FLOAT, myid - <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD,</span><br><span class="line">            	&amp;status);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            MPI_Sendrecv(&amp;myRows[SIZE][<span class="number">0</span>], N, MPI_FLOAT, myid + <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	&amp;myRows[<span class="number">0</span>][<span class="number">0</span>], N, MPI_FLOAT, myid - <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD,</span><br><span class="line">            	&amp;status);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 从下向上平移数据</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="number">3</span> == myid) &#123;</span><br><span class="line">            MPI_Send(&amp;myRows[<span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, myid - <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">0</span> == myid) &#123;</span><br><span class="line">            MPI_Recv(&amp;myRows[SIZE + <span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, myid + <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	MPI_COMM_WORLD, &amp;status);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            MPI_Sendrecv(&amp;myRows[<span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, myid - <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	&amp;myRows[SIZE + <span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, myid + <span class="number">1</span>, <span class="number">0</span>,</span><br><span class="line">            	MPI_COMM_WORLD, &amp;status);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 计算</span></span><br><span class="line">        <span class="keyword">int</span> r_begin, r_end;</span><br><span class="line">        r_begin = (<span class="number">0</span> == myid) ? <span class="number">2</span> : <span class="number">1</span>;</span><br><span class="line">        r_end = (<span class="number">3</span> == myid) ? SIZE - <span class="number">1</span> : SIZE;</span><br><span class="line">        <span class="keyword">for</span> (i = r_begin; i &lt;= r_end; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt; N - <span class="number">1</span>; j++)</span><br><span class="line">                myRows2[i][j] = <span class="number">0.25</span> * (myRows[i][j - <span class="number">1</span>] + myRows[i][j + <span class="number">1</span>] +</span><br><span class="line">                					    myRows[i - <span class="number">1</span>][j] + myRows[i + <span class="number">1</span>][j]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 更新</span></span><br><span class="line">        <span class="keyword">for</span> (i = r_begin; i &lt;= r_end; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt; N - <span class="number">1</span>; j++)</span><br><span class="line">                myRows[i][j] = myRows2[i][j];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    MPI_Barrier(MPI_COMM_WORLD);</span><br><span class="line">    print_myRows(myid, myRows);</span><br><span class="line">    MPI_Finalize();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_myRows</span><span class="params">(<span class="keyword">int</span> myid, <span class="keyword">float</span> myRows[][N])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line">    <span class="keyword">int</span> buf[<span class="number">1</span>];</span><br><span class="line">    MPI_Status status;</span><br><span class="line">    buf[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> ( myid&gt;<span class="number">0</span> ) &#123;</span><br><span class="line">        MPI_Recv(buf, <span class="number">1</span>, MPI_INT, myid - <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD, &amp;status);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"Result in process %d:\n"</span>, myid);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; SIZE + <span class="number">2</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"%1.3f\t"</span>, myRows[i][j]);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ( myid&lt;<span class="number">3</span> ) &#123;</span><br><span class="line">        MPI_Send(buf, <span class="number">1</span>, MPI_INT, myid + <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD);</span><br><span class="line">    &#125;</span><br><span class="line">    MPI_Barrier(MPI_COMM_WORLD);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="引入虚进程"><a href="#引入虚进程" class="headerlink" title="引入虚进程"></a>引入虚进程</h3><p>由于首进程和尾进程需要特殊处理，在实际写程序的时候还是比较麻烦的，因此MPI使用了虚进程。比如顺序传递数据，0-&gt;1，1-&gt;2，2-&gt;3，3-&gt;4，4-&gt;MPI_PROC_NULL，这样就比较方便了。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 8</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SIZE (N / 4)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T 2</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_myRows</span><span class="params">(<span class="keyword">int</span>, <span class="keyword">float</span> [][N])</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">float</span> myRows[SIZE + <span class="number">2</span>][N], myRows2[SIZE + <span class="number">2</span>][N];</span><br><span class="line">    <span class="keyword">int</span> myid;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    MPI_Init(&amp;argc, &amp;argv);</span><br><span class="line">    MPI_Comm_rank(MPI_COMM_WORLD, &amp;myid);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line">    <span class="comment">// 初始化</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; SIZE + <span class="number">2</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">        &#123;</span><br><span class="line">            myRows[i][j] = myRows2[i][j] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">0</span> == myid) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            myRows[<span class="number">1</span>][j] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">3</span> == myid) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            myRows[SIZE][j] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; SIZE + <span class="number">1</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        myRows[i][<span class="number">0</span>] = <span class="number">8.0</span>;</span><br><span class="line">        myRows[i][N - <span class="number">1</span>] = <span class="number">8.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Jacobi Iteration部分</span></span><br><span class="line">    <span class="keyword">int</span> upid, downid;</span><br><span class="line">    <span class="comment">// 向上传递的ID，如果从0号进程传递，则上方为MPI_PROC_NULL</span></span><br><span class="line">    upid = (<span class="number">0</span> == myid) ? MPI_PROC_NULL : myid - <span class="number">1</span>;</span><br><span class="line">    <span class="comment">// 向下传递的ID，如果从SIZE - 1号进程传递，则下方为MPI_PROC_NULL</span></span><br><span class="line">    downid = (<span class="number">3</span> == myid) ? MPI_PROC_NULL : myid + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">int</span> <span class="built_in">step</span>;</span><br><span class="line">    <span class="keyword">for</span> ( <span class="built_in">step</span> = <span class="number">0</span>; <span class="built_in">step</span> &lt; T; <span class="built_in">step</span>++ )</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 从上向下平移数据</span></span><br><span class="line">        MPI_Sendrecv(&amp;myRows[SIZE][<span class="number">0</span>], N, MPI_FLOAT, downid, <span class="number">0</span>, &amp;myRows[<span class="number">0</span>][<span class="number">0</span>],</span><br><span class="line">        	N, MPI_FLOAT, upid, <span class="number">0</span>, MPI_COMM_WORLD, &amp;status);</span><br><span class="line">        <span class="comment">// 从下往上发送数据</span></span><br><span class="line">        MPI_Sendrecv(&amp;myRows[<span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, upid, <span class="number">0</span>,</span><br><span class="line">        	&amp;myRows[SIZE + <span class="number">1</span>][<span class="number">0</span>], N, MPI_FLOAT, downid, <span class="number">0</span>, MPI_COMM_WORLD,</span><br><span class="line">        	&amp;status);</span><br><span class="line">        <span class="comment">// 计算</span></span><br><span class="line">        <span class="keyword">int</span> r_begin, r_end;</span><br><span class="line">        r_begin = (<span class="number">0</span> == myid) ? <span class="number">2</span> : <span class="number">1</span>;</span><br><span class="line">        r_end = (<span class="number">3</span> == myid) ? SIZE - <span class="number">1</span> : SIZE;</span><br><span class="line">        <span class="keyword">for</span> (i = r_begin; i &lt;= r_end; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt; N - <span class="number">1</span>; j++)</span><br><span class="line">                myRows2[i][j] = <span class="number">0.25</span> * (myRows[i][j - <span class="number">1</span>] + myRows[i][j + <span class="number">1</span>] + myRows[i - <span class="number">1</span>][j] + myRows[i + <span class="number">1</span>][j]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 更新</span></span><br><span class="line">        <span class="keyword">for</span> (i = r_begin; i &lt;= r_end; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt; N - <span class="number">1</span>; j++)</span><br><span class="line">                myRows[i][j] = myRows2[i][j];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    MPI_Barrier(MPI_COMM_WORLD);</span><br><span class="line">    print_myRows(myid, myRows);</span><br><span class="line">    MPI_Finalize();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_myRows</span><span class="params">(<span class="keyword">int</span> myid, <span class="keyword">float</span> myRows[][N])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line">    <span class="keyword">int</span> buf[<span class="number">1</span>];</span><br><span class="line">    MPI_Status status;</span><br><span class="line">    buf[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> ( myid&gt;<span class="number">0</span> ) &#123;</span><br><span class="line">        MPI_Recv(buf, <span class="number">1</span>, MPI_INT, myid - <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD, &amp;status);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"Result in process %d:\n"</span>, myid);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; SIZE + <span class="number">2</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; N; j++)</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"%1.3f\t"</span>, myRows[i][j]);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ( myid&lt;<span class="number">3</span> ) &#123;</span><br><span class="line">        MPI_Send(buf, <span class="number">1</span>, MPI_INT, myid + <span class="number">1</span>, <span class="number">0</span>, MPI_COMM_WORLD);</span><br><span class="line">    &#125;</span><br><span class="line">    MPI_Barrier(MPI_COMM_WORLD);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="主从模式的MPI程序设计"><a href="#主从模式的MPI程序设计" class="headerlink" title="主从模式的MPI程序设计"></a>主从模式的MPI程序设计</h2><h3 id="矩阵相乘"><a href="#矩阵相乘" class="headerlink" title="矩阵相乘"></a>矩阵相乘</h3><p>其实这里我不太明白在master进程中初始化b然后对b进行广播和直接在所有进程中对b进行初始化的区别。待查……</p>
<p>思考：如果是同样的计算，在一个进程中计算然后再广播应该比在多个进程同时计算要省计算量（电费）。</p>
<p>这个还是没太看懂……</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ROWS 100</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> COLS 100</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> min(x,y) ((x)&gt;(y)?(y):(x))</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> rows = <span class="number">10</span>, cols = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">int</span> master = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> myid, numprocs;</span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line">    <span class="keyword">float</span> a[ROWS][COLS], b[COLS], c[COLS];</span><br><span class="line">    <span class="keyword">float</span> row_result;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    MPI_Init(&amp;argc, &amp;argv);</span><br><span class="line">    MPI_Comm_rank(MPI_COMM_WORLD, &amp;myid);</span><br><span class="line">    MPI_Comm_size(MPI_COMM_WORLD, &amp;numprocs);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*master进程*/</span></span><br><span class="line">    <span class="keyword">if</span> (master == myid) &#123;</span><br><span class="line">        <span class="comment">/*初始化矩阵a和b*/</span></span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; cols; j++) b[j] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; rows; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; cols; j++)</span><br><span class="line">            &#123;</span><br><span class="line">                a[i][j] = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/*只在master进程中初始化b 其余slave进程通过被广播的形式获得向量b*/</span></span><br><span class="line">        MPI_Bcast(&amp;b[<span class="number">0</span>], cols, MPI_FLOAT, master, MPI_COMM_WORLD);</span><br><span class="line">        <span class="comment">/*向各个slave进程发送矩阵a的各行*/</span></span><br><span class="line">        <span class="keyword">int</span> numsent = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; <span class="built_in">min</span>(numprocs,rows + <span class="number">1</span>); i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/* 每个slave进程计算一行×一列的结果，这里用MPI_TAG参数标示对应结果向量c的下标+1。MPI_TAG在这里的开始取值范围是1，要把MPI_TAG空出来，作为结束slave进程的标志 */</span></span><br><span class="line">            MPI_Send(&amp;a[i - <span class="number">1</span>][<span class="number">0</span>], cols, MPI_FLOAT, i, ++numsent,</span><br><span class="line">            	MPI_COMM_WORLD);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/*master进程接受其他各进程的计算结果*/</span></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; rows; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/*类似poll的方法 只要有某个slave进程算出来结果了</span></span><br><span class="line"><span class="comment">            MPI_Recv就能返回执行一次*/</span></span><br><span class="line">            MPI_Recv(&amp;row_result, <span class="number">1</span>, MPI_FLOAT, MPI_ANY_SOURCE, MPI_ANY_TAG,</span><br><span class="line">            	MPI_COMM_WORLD, &amp;status);</span><br><span class="line">            <span class="comment">/*这里MPI_TAG的范围是1到rows 注意存储结果时下标减1*/</span></span><br><span class="line">            c[status.MPI_TAG - <span class="number">1</span>] = row_result;</span><br><span class="line">            <span class="comment">/*发送矩阵a中没发送完的行 就用刚返回计算结果空出来的那个slave进程</span></span><br><span class="line"><span class="comment">            通过status.MPI_SOURCE找到这个空出来的进程*/</span></span><br><span class="line">            <span class="keyword">if</span> (numsent &lt; rows) &#123;</span><br><span class="line">                MPI_Send(&amp;a[numsent][<span class="number">0</span>], cols, MPI_FLOAT, status.MPI_SOURCE,</span><br><span class="line">                	numsent + <span class="number">1</span>, MPI_COMM_WORLD);</span><br><span class="line">                numsent = numsent + <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123; <span class="comment">/*发送空消息 关闭slave进程*/</span></span><br><span class="line">                <span class="keyword">float</span> <span class="built_in">close</span> = <span class="number">1.0</span>;</span><br><span class="line">                MPI_Send(&amp;<span class="built_in">close</span>, <span class="number">1</span>, MPI_FLOAT, status.MPI_SOURCE, <span class="number">0</span>,</span><br><span class="line">                	MPI_COMM_WORLD);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/*打印乘法结果*/</span></span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; cols; j++ )</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"%1.3f\t"</span>, c[j]);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/*slave进程*/</span></span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">    	<span class="comment">// slave进程需要同样执行MPI_Bcast才能实现广播</span></span><br><span class="line">        MPI_Bcast(&amp;b[<span class="number">0</span>], cols, MPI_FLOAT, master, MPI_COMM_WORLD);</span><br><span class="line">        <span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            row_result = <span class="number">0</span>;</span><br><span class="line">            <span class="comment">// 用c接收a矩阵中对应行的内容</span></span><br><span class="line">            MPI_Recv(&amp;c[<span class="number">0</span>], cols, MPI_FLOAT, master, MPI_ANY_TAG,</span><br><span class="line">            	MPI_COMM_WORLD, &amp;status);</span><br><span class="line">            <span class="keyword">if</span> (<span class="number">0</span> != status.MPI_TAG ) &#123;</span><br><span class="line">                <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; cols; j++ )</span><br><span class="line">                &#123;</span><br><span class="line">                    row_result = row_result + b[j]*c[j];</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// printf("myid:%d, MPI_TAG:%d, c[0]:%f, row_result:%1.3f\n",</span></span><br><span class="line">                <span class="comment">// myid, status.MPI_TAG,c[0], row_result);</span></span><br><span class="line">                <span class="comment">// 主进程发送数据的时候对N号进程MPI_TAG就是N</span></span><br><span class="line">                MPI_Send(&amp;row_result, <span class="number">1</span>, MPI_FLOAT, master, status.MPI_TAG,</span><br><span class="line">                	MPI_COMM_WORLD);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    MPI_Finalize();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="打印信息"><a href="#打印信息" class="headerlink" title="打印信息"></a>打印信息</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MSG_EXIT 1</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MSG_ORDERED 2</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MSG_UNORDERED 3</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">master_io</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">slaver_io</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> rank, <span class="built_in">size</span>;</span><br><span class="line">    MPI_Init(&amp;argc, &amp;argv);</span><br><span class="line">    MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">0</span> == rank) &#123;</span><br><span class="line">        master_io();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        slaver_io();</span><br><span class="line">    &#125;</span><br><span class="line">    MPI_Finalize();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">master_io</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i, j, <span class="built_in">size</span>, nslaver, firstmsg;</span><br><span class="line">    <span class="keyword">char</span> buf[<span class="number">256</span>], buf2[<span class="number">256</span>];</span><br><span class="line">    MPI_Status status;</span><br><span class="line">    MPI_Comm_size(MPI_COMM_WORLD, &amp;<span class="built_in">size</span>);</span><br><span class="line">    nslaver = <span class="built_in">size</span> - <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span> (nslaver &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        MPI_Recv(buf, <span class="number">256</span>, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG,</span><br><span class="line">        	MPI_COMM_WORLD, &amp;status);</span><br><span class="line">        <span class="keyword">switch</span>(status.MPI_TAG)&#123;</span><br><span class="line">            <span class="keyword">case</span> MSG_EXIT:</span><br><span class="line">                nslaver--;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> MSG_UNORDERED: <span class="comment">// 如果是乱序就直接输出</span></span><br><span class="line">                <span class="built_in">fputs</span>(buf, <span class="built_in">stdout</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> MSG_ORDERED:</span><br><span class="line">                firstmsg = status.MPI_SOURCE;</span><br><span class="line">                <span class="comment">/* 这段程序设计的比较巧妙</span></span><br><span class="line"><span class="comment">                 * 虽然每个slaver发送两个message 不同slaver的message可能有重叠</span></span><br><span class="line"><span class="comment">                 * 但是每个每个slaver每次只能被接收一个message</span></span><br><span class="line"><span class="comment">                 * 这一轮一旦接收到message了</span></span><br><span class="line"><span class="comment">                 * 就不再从这个slaver接收消息了</span></span><br><span class="line"><span class="comment">                 * 概括说:</span></span><br><span class="line"><span class="comment">                 * 第一次进入MSG_ORDERED处理各个slaver第一次调用MPI_Send发送的消息</span></span><br><span class="line"><span class="comment">                 * 第二次进入MSG_ORDERED处理各个slaver第二次调用MPI_Send发送的消息*/</span></span><br><span class="line">                <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; <span class="built_in">size</span>; i++)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">if</span> (i==firstmsg) &#123;</span><br><span class="line">                        <span class="built_in">fputs</span>(buf, <span class="built_in">stdout</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">else</span> &#123;</span><br><span class="line">                        MPI_Recv(buf2, <span class="number">256</span>, MPI_CHAR, i, MSG_ORDERED, MPI_COMM_WORLD, &amp;status);</span><br><span class="line">                        <span class="built_in">fputs</span>(buf2, <span class="built_in">stdout</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">slaver_io</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> buf[<span class="number">256</span>];</span><br><span class="line">    <span class="keyword">int</span> rank;</span><br><span class="line">    MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);</span><br><span class="line">    <span class="comment">/*第一次向master进程发送有序消息*/</span></span><br><span class="line">    <span class="built_in">sprintf</span>(buf, <span class="string">"Hello from slaver %d, ordered print\n"</span>, rank);</span><br><span class="line">    MPI_Send(buf, <span class="built_in">strlen</span>(buf) + <span class="number">1</span>, MPI_CHAR, <span class="number">0</span>, MSG_ORDERED, MPI_COMM_WORLD);</span><br><span class="line">    <span class="comment">/*第二次向master进程发送有序消息*/</span></span><br><span class="line">    <span class="built_in">sprintf</span>(buf, <span class="string">"Bye from slaver %d, ordered print\n"</span>, rank);</span><br><span class="line">    MPI_Send(buf, <span class="built_in">strlen</span>(buf) + <span class="number">1</span>, MPI_CHAR, <span class="number">0</span>, MSG_ORDERED, MPI_COMM_WORLD);</span><br><span class="line">    <span class="comment">/*第一次向master发送无序消息*/</span></span><br><span class="line">    <span class="built_in">sprintf</span>(buf, <span class="string">"I'm exiting (%d), unordered print\n"</span>, rank);</span><br><span class="line">    MPI_Send(buf, <span class="built_in">strlen</span>(buf) + <span class="number">1</span>, MPI_CHAR, <span class="number">0</span>, MSG_UNORDERED, MPI_COMM_WORLD);</span><br><span class="line">    <span class="comment">/*发送结束信息*/</span></span><br><span class="line">    MPI_Send(buf, <span class="number">0</span>, MPI_CHAR, <span class="number">0</span>, MSG_EXIT, MPI_COMM_WORLD);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
      
  <div class="popular-posts-header">相关文章</div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/35856/" rel="bookmark">LBM-MPI测试程序</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/50341/" rel="bookmark">MPI进程通讯头文件</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/59233/" rel="bookmark">MPI程序的GDB调试</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/22859/" rel="bookmark">MPI三维数据传递</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/18454/" rel="bookmark">MPI学习记录（一）</a></div>
    </li>
  </ul>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>ChiangYung
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chiangyung.github.io/posts/55404/" title="MPI学习记录（二）">https://chiangyung.github.io/posts/55404/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Programming/" rel="tag"><i class="fa fa-tag"></i> Programming</a>
              <a href="/tags/MPI/" rel="tag"><i class="fa fa-tag"></i> MPI</a>
              <a href="/tags/Parallel-Computing/" rel="tag"><i class="fa fa-tag"></i> Parallel Computing</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/18454/" rel="prev" title="MPI学习记录（一）">
      <i class="fa fa-chevron-left"></i> MPI学习记录（一）
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/22859/" rel="next" title="MPI三维数据传递">
      MPI三维数据传递 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#对等模式的MPI程序设计"><span class="nav-number">1.</span> <span class="nav-text">对等模式的MPI程序设计</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#初始设计"><span class="nav-number">1.1.</span> <span class="nav-text">初始设计</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#捆绑发送"><span class="nav-number">1.2.</span> <span class="nav-text">捆绑发送</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#引入虚进程"><span class="nav-number">1.3.</span> <span class="nav-text">引入虚进程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#主从模式的MPI程序设计"><span class="nav-number">2.</span> <span class="nav-text">主从模式的MPI程序设计</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#矩阵相乘"><span class="nav-number">2.1.</span> <span class="nav-text">矩阵相乘</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#打印信息"><span class="nav-number">2.2.</span> <span class="nav-text">打印信息</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="ChiangYung"
      src="/upload/E8.png">
  <p class="site-author-name" itemprop="name">ChiangYung</p>
  <div class="site-description" itemprop="description">我于蒙昧之中，探寻宇宙万物。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">41</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chiangyung" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chiangyung" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:chiangyoung@outlook.com" title="E-Mail → mailto:chiangyoung@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://openfoamwiki.net/index.php/Main_Page" title="http:&#x2F;&#x2F;openfoamwiki.net&#x2F;index.php&#x2F;Main_Page" rel="noopener" target="_blank">OpenFOAMWiki</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ChiangYung</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">207k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:08</span>
</div>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
    <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>

<script src="/js/kity.min.js"></script>

<script src="/js/kityminder.core.min.js"></script>

<script src="/js/mindmap.js"></script>

<script src="/js/mindmap.min.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  

</body>
</html>
